package cn.com.headfree.evaluation.export;

import cn.com.headfree.entity.*;
import cn.com.headfree.evaluation.dao.IDeclareEvaluationDao;
import cn.com.headfree.evaluation.export.vo.EvaluationDeclareReportVo;
import cn.com.headfree.expert.dao.IDeclareExpertDao;
import cn.com.headfree.expert.dao.IUserDataForExpertDao;
import cn.com.headfree.expert.service.IUserDataInfoServiceThreeYear;
import cn.com.headfree.util.SysDictionaryUtil;
import cn.com.headfree.vo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("evaluationDeclareUserDataInfoService")
public class EvaluationDeclareUserDataInfoService {

	private static final Logger LOG = LogManager.getLogger(EvaluationDeclareUserDataInfoService.class);

    @Autowired
    private IUserDataForExpertDao userDataForExpertDao;
    @Autowired
    private IDeclareEvaluationDao declareExpertDao;
    @Autowired
    private IUserDataInfoServiceThreeYear userDataInfoService;


	public EvaluationDeclareReportVo getModelData(String erpNo,String declareId, Date acquTime) {
        List<TPartRank> tPartRankList = new ArrayList<>();
        TPartRank tPartRank=new TPartRank();
        tPartRank.setErpNo(erpNo);
        tPartRankList.add(tPartRank);
        TDeclareEvaluationInfo declareEvaluationInfo = declareExpertDao.getTDeclareEvaluationInfoById(declareId);
        //基本信息
        TEmpBaseInfo tEmpBaseInfo = userDataForExpertDao.getBaseInfoByErpNo(erpNo);
        //最高学历信息
        List<EducateInfoVo> highEdus = userDataForExpertDao.getEducateInfoHighEducByErpNo(erpNo);
        //近三年绩效
        List<YearEffectVo> yearEffectVos = userDataForExpertDao.getYearEffectByErpNo(erpNo);
        //主要工作经历
        List<WorkExpInfoVo> workExpInfoVos = userDataForExpertDao.getWorkStatusExpInfoByErpNo(erpNo);
        //成果获奖积分（科技管理创新）
        TTecManInnov tTecManInnov=new TTecManInnov();
        tTecManInnov.setErpNo(erpNo);
        tTecManInnov.settPartRankList(tPartRankList);
        List<TTecManInnovVo>  tecManInnovVos = userDataInfoService.findPageTecManInnovByCondi(tTecManInnov);
        if(CollectionUtils.isNotEmpty(tecManInnovVos)) {
            tecManInnovVos.forEach(p -> {
                        p.setRoleName(SysDictionaryUtil.getNameByValueForDefault("XueshuchengguoJs", p.getRole()));
                        p.setAwardLevelName(SysDictionaryUtil.getNameByValueForDefault("JygexinQcJb", p.getAwardLevel()));
                        p.setAwardGradeName(SysDictionaryUtil.getNameByValueForDefault("JygexinQcDj", p.getAwardGrade()));
                        p.setAchiTypeName(SysDictionaryUtil.getNameByValueForDefault("KjglAchiveType", p.getAchiType()));
                    }
            );
        }
        //授权专利
        TPatentInfo tPatentInfo = new TPatentInfo();
        tPatentInfo.setErpNo(erpNo);
        tPatentInfo.settPartRankList(tPartRankList);
        List<TPatentInfoVo> patentVos = userDataInfoService.findPagePatentInfoByCondi(tPatentInfo);
        if (!patentVos.isEmpty()) {
            patentVos.forEach(p -> {
                p.setRoleName(SysDictionaryUtil.getNameByValueForDefault("XueshuchengguoJs", p.getRole()));
                p.setPatentTypeName(SysDictionaryUtil.getNameByValueForDefault("PatentType", p.getPatentType()));
            });
        }
        //论文专著
        TAcadeAchiveInfo tAcadeAchiveInfo = new TAcadeAchiveInfo();
        tAcadeAchiveInfo.setErpNo(erpNo);
        tAcadeAchiveInfo.settPartRankList(tPartRankList);
        List<TAcadeAchiveInfoVo> acadeAchiveVos =  userDataInfoService.findPageAcadeAchiveInfoByCondi(tAcadeAchiveInfo);
        if(!acadeAchiveVos.isEmpty()) {
            acadeAchiveVos.forEach(p -> {
                p.setRoleName(SysDictionaryUtil.getNameByValueForDefault("XueshuchengguoJs", p.getRole()));
                p.setAcadTypeName(SysDictionaryUtil.getNameByValueForDefault("XueshuchengguoType", p.getAcadType()));
                p.setLevelName(SysDictionaryUtil.getNameByValueForDefault("XueshuchengguoJb", p.getLevel()));
            });
        }
        //制度规范
        TStandInfo tStandInfo = new TStandInfo();
        tStandInfo.setErpNo(erpNo);
        tStandInfo.settPartRankList(tPartRankList);
        List<TStandInfoVo> standInfoVos = userDataInfoService.findPageStandInfoByCondi(tStandInfo);
        if (!standInfoVos.isEmpty()) {
            standInfoVos.forEach(p -> {
                p.setRoleName(SysDictionaryUtil.getNameByValueForDefault("XueshuchengguoJs", p.getRole()));
                p.setTypeName(SysDictionaryUtil.getNameByValueForDefault("ZdbzType", p.getType()));
                p.setLevelName(SysDictionaryUtil.getNameByValueForDefault("JygexinQcJb", p.getLevel()));
            });
        }
        //个人荣誉
        THonorInfo tHonorInfo = new THonorInfo();
        tHonorInfo.setErpNo(erpNo);
        List<THonorInfo> honorDatasVos = userDataInfoService.findPageHonorInfoByCondi(tHonorInfo);
        if (!honorDatasVos.isEmpty()) {
            honorDatasVos.forEach(p -> {
                p.setHonorLevelName(SysDictionaryUtil.getNameByValueForDefault("JygexinQcJb", p.getHonorLevel()));
                p.setHonorTypeName(SysDictionaryUtil.getNameByValueForDefault("RuYuLeiBie", p.getHonorType()));
            });
        }
        //培训授课--培训部分
//		List<StudyAndTrainVo> studyAndTrainVos = userDataForExpertDao.getContiStudyInfoByErpNo(erpNo);
        TContiStudyInfo tContiStudyInfo = new TContiStudyInfo();
        tContiStudyInfo.setErpNo(erpNo);
        List<TContiStudyInfo> studyAndTrainVos =  userDataInfoService.findPageContiStudyInfoByCondi(tContiStudyInfo);
        //技术规范
        TTecStandardInfo tecStandardInfo = new  TTecStandardInfo();
        tecStandardInfo.setErpNo(erpNo);
        tecStandardInfo.settPartRankList(tPartRankList);
        List<TTecStandardInfo> tecStandardInfoVos = userDataInfoService.findPageTecStandardInfoByCondi(tecStandardInfo);
        if (!tecStandardInfoVos.isEmpty()) {
            tecStandardInfoVos.forEach(p -> {
                p.setTypeName(SysDictionaryUtil.getNameByValueForDefault("tecStandardRole", p.getType()));
                p.setLevelName(SysDictionaryUtil.getNameByValueForDefault("TecStandardType", p.getLevel()));
                p.setStandardTypeStr(SysDictionaryUtil.getNameByValueForDefault("tecStandardType", p.getStandardType()));
            });
        }
        //拼装数据
        EvaluationDeclareReportVo declareExpertReportVo = new EvaluationDeclareReportVo();
        LOG.info("===============组装模板数据开始=================");
        declareExpertReportVo.setDeclareEvaluationInfo(declareEvaluationInfo);
        declareExpertReportVo.settEmpBaseInfo(tEmpBaseInfo);
        if(CollectionUtils.isNotEmpty(highEdus)){
            declareExpertReportVo.setHighEdus(highEdus.get(0));
        }
        if(CollectionUtils.isNotEmpty(yearEffectVos)){
            declareExpertReportVo.setYearEffectVos(yearEffectVos);
        }
        if(CollectionUtils.isNotEmpty(workExpInfoVos)){
            declareExpertReportVo.setWorkExpInfoVos(workExpInfoVos);
        }
        if(CollectionUtils.isNotEmpty(tecManInnovVos)){
            declareExpertReportVo.setTecManInnovVos(tecManInnovVos);
        }
        if(CollectionUtils.isNotEmpty(patentVos)){
            declareExpertReportVo.setPatentInfoVos(patentVos);
        }
        if(CollectionUtils.isNotEmpty(acadeAchiveVos)){
            declareExpertReportVo.setAcadeAchiveVos(acadeAchiveVos);
        }
        if(CollectionUtils.isNotEmpty(standInfoVos)){
            declareExpertReportVo.setStandInfoVos(standInfoVos);
        }

        if(CollectionUtils.isNotEmpty(honorDatasVos)){
            declareExpertReportVo.setHonorDatasVos(honorDatasVos);
        }
        if(CollectionUtils.isNotEmpty(studyAndTrainVos)){
            declareExpertReportVo.setStudyAndTrainVos(studyAndTrainVos);
        }
        if(CollectionUtils.isNotEmpty(tecStandardInfoVos)){
            declareExpertReportVo.setTecStandardInfoVos(tecStandardInfoVos);
        }
        LOG.info("======================数据组装完成========================");
        return declareExpertReportVo;
    }

}
